#include "dca700.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>



#ifdef 	CHECK_POWER_DOWN
	int mfd = -1;
	unsigned char *cpld_base = NULL;
	#define GPMC_CS3_BASE			0x10000000
	#define MAP_LENGTH			0x400

	#define WDG_REG_OFFSET			0x0
	#define BUZZER_REG_OFFSET		0x2
//	#define DIO_IN_REG_OFFSET		0x3  //DCA210: 0x3   DCA700:  0x06

	#define PCIE_CONTROL_REG_OFFSET		0xe
	#define SIMCARD_REG_OFFSET		0x44
	#define SYSTEM_ID_REG_OFFSET		0xc0

	#define WDG_EN_BIT			(0x1 << 0)
	#define WDG_FD_BIT			(0x1 << 1)
	#define BUZZER_EN_BIT			(0x1 << 0)
	//#define PWR_12V_FAIL_BIT		(0x1 << 0)  //DCA210_V2.3: 0x1 << 0  DCA700:0x1 << 6
	#define PCIE_PWR_4G_EN			(0x1 << 0)
	#define PCIE_USIM_SEL_BIT		(0x1 << 2)
	#define SIM0_INSERT_BIT			(0x1 << 0)
	#define SIM1_INSERT_BIT			(0x1 << 1)


	#define DIO_IN_REG_OFFSET_210		0x03  //DCA210: 0x3   DCA700:  0x06
	#define PWR_12V_FAIL_BIT_210		(0x1 << 0)  //DCA210_V2.3: 0x1 << 0  DCA700:0x1 << 6

	#define DIO_IN_REG_OFFSET_700		0x06  //DCA210: 0x3   DCA700:  0x06
	#define PWR_12V_FAIL_BIT_700		(0x1 << 6)  //DCA210_V2.3: 0x1 << 0  DCA700:0x1 << 6

	#define DIO_IN_REG_OFFSET_220		0x12  //DCA220: 0x12
	#define PWR_12V_FAIL_BIT_220		(0x1 << 0)

//------------------------------------------------------------------------------------
//	CPLD内存映射。
//------------------------------------------------------------------------------------
int cpld_mmap_init(void)
{
	//unsigned char ret = 0;
	mfd = open("/dev/mem", O_RDWR);
	if(mfd < 0 ) {
		printf("----open mem failed.\n");
		return -1;
	}

	cpld_base = (unsigned char *)mmap(NULL, MAP_LENGTH, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, GPMC_CS3_BASE);
	if(NULL == cpld_base) {
		printf("----mmap GPMC_CS3_BASE failed.\n");
		close(mfd);
		return -2;
	}

	return 0;
}

////------------------------------------------------------------------------------------
////	CPLD内存去映射。
////------------------------------------------------------------------------------------
//static void cpld_mmap_exit(void)
//{
//	//pthread_join(thread_sim, NULL);
//	munmap(cpld_base, MAP_LENGTH);
//	close(mfd);
//}

//------------------------------------------------------------------------------------
//	读取硬件寄存器的内容。
// 	offset: 地址偏移量
//------------------------------------------------------------------------------------
static unsigned char read_cpld_reg(unsigned char offset)
{
	return (*(cpld_base + offset));
}

////------------------------------------------------------------------------------------
////	向硬件寄存器写入一定的值。
//// 	offset: 地址偏移量
//// 	val:	写入的值
////------------------------------------------------------------------------------------
//static void write_cpld_reg(unsigned char offset, unsigned char value)
//{
//	*(cpld_base + offset) = value;
//}

//检测系统外部电源是否掉电
int check_power_12v(char hardver)
{
	int ret = 0;

	char offset;
	char bit;
	if(hardver== DCA210)
	{
		offset=DIO_IN_REG_OFFSET_210;
		bit=PWR_12V_FAIL_BIT_210;
	}
	else if (hardver== DCA220)
	{
		offset = DIO_IN_REG_OFFSET_220;
		bit    = PWR_12V_FAIL_BIT_220;
	}
	else
	{
		offset=DIO_IN_REG_OFFSET_700;
		bit=PWR_12V_FAIL_BIT_700;
	}

	ret = read_cpld_reg(offset);

//	while (ret == 0xff) {
//		printf("----Read DIO_IN_REG_OFFSET failed!\n");
//		sleep(1);
//		ret = read_cpld_reg(DIO_IN_REG_OFFSET);
//	}

	ret &= bit;
	if (ret) {
		return 1;
	} else {
		return 0;
	}
}

//int main()
//{
//	if(cpld_mmap_init() < 0) {
//		printf("----CPLD memory init failed!\n");
//		return -1;
//	}
//
//	while(1) {
//		if(check_power_12v() > 0)
//			printf("----System Power is ON!\n");
//		else
//			printf("----System Power is OFF!\n");
//
//		sleep(1);
//	}
//
//	cpld_mmap_exit();
//	return 0;
//}
#endif
